﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.EnterCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.EnterCommandHandler
{
    public class UpdateCommandHandler : IRequestHandler<UpdateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_EnterInfo> enterRep;
        public UpdateCommandHandler(IBaseRepository<SU_EnterInfo> enterRep)
        {
            this.enterRep = enterRep;
        }
        public Task<APIResult<int>> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list= enterRep.GetAll().Result.Where(x=>x.EnterId==request.EnterId).FirstOrDefault();
            list.EnterId=request.EnterId;
            list.ProductionCode = request.ProductionCode;
            list.FarmAddr = request.FarmAddr;
            list.PersonName = request.PersonName;
            list.EnterNum = request.EnterNum;
            list.Qualifications = request.Qualifications;
            list.EnterName = request.EnterName;
            list.CreateEnterTime = request.CreateEnterTime;
            list.EnterAddress = request.EnterAddress;
            list.EnterRemark = request.EnterRemark;
            list.EnterPhones = request.EnterPhones;
            var res = enterRep.Update(list).Result;
            if (res > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
